<?php
class moduleController extends adminController{
    protected $layout = 'layout';
    //管理模型表
    public function index(){
        $act = in($_GET['act']);
        if($act == 'del'){
            $fid = intval($_GET['id']);
            $msg = model('module')->delTable($fid);
            if($msg == 1){
                echo 1;
            }else{
                echo $msg;
            }
        }else{
            //分页开始
            $listRows = 15; //每页条数
            $url = url('module/index',array('page'=>'{page}'));
            $allcount = model('module')->count();
            $limit = $this->pageLimit($url,$listRows);
            $this->page= $this->pageShow($allcount);
            //分页结束
            $this->list = model('module')->select(null,null,null,$limit); 
            $this->display('module/index'); 
        }
    }
    //添加模型表
    public function add(){
        if($this->isPost()){
            $msg = Check::rule(
                    array(check::must($_POST['name']),'模型名称必须填写'),
                    array(check::must($_POST['tables']),'模型表名必须填写'),
                    array(check::userName($_POST['tables'],3,20,'EN'),'只能输入3至20个纯英文字母'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $data = array();
            $data['name']   = $_POST['name']; //模型名称
            $data['type']  = intval($_POST['type']);
            $data['tables']  = 'cms_ext_' . $_POST['tables']; //表名
            $data['uptime'] = time(); 
            $result = model('module')->addModule($data);
            if($result){
                $this->jsonMsg('模型添加成功',1);
            }else{
                $this->jsonMsg('模型添加失败');
            }
        }else{
            $this->display('module/add');
        }
    }
    //编辑模型表
    public function edit(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['name']),'模型名称必须填写'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $fid = intval($_POST['fid']);
            $data = array();
            $data['name'] = in($_POST['name']);
            $data['uptime'] = time();
            $result = model('module')->editModule($fid,$data);
            if($result){
                $this->jsonMsg('模型修改成功',1);
            }else{
                $this->jsonMsg('模型修改失败');
            }
        }else{
            $fid = intval($_GET['id']);
            $this->info = model('module')->find('id = '.$fid,'id,name');
            $this->display('module/edit'); 
        }
    }
    //判断模型是否存在
    public function modulename(){
        $name = $_POST['name'];
        $param = $_POST['param'];
        if(!$name || !$param){
            $info['info']= '模型表名不能空！';
            $info['status']= 'n';
            echo json_encode($info);
        }else{
            $result = model('module')->find('tables="cms_ext_'.$param.'"','tables');
            if($result){
                $info['info']= '相同表已经存在,请更换其它!';
                $info['status']= 'n';
                echo json_encode($info);
            }else{
                $info['info']= '相同表不存在,可以提交!';
                $info['status']= 'y';
                echo json_encode($info);
            }  
        }
    }
    //################################################
    //管理模型字段
    public function field(){
        $act = in($_GET['act']);
        if($act == 'del'){
            $fid = intval($_GET['id']);
            $result = model('field')->delField($fid);
            if($result){
                echo 1;
            }else{
                echo '删除失败';
            }
        }else{
            $mid = intval($_GET['id']);
            $this->info = model('module')->find('id='.$mid,'id,name'); 
            $this->list = model('field')->select('mid ='.$mid,0,'neworder asc,id asc');
            $this->display('module/field');  
        }
    }
    //添加模型字段
    public function fieldadd(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['mid']),'模型ID不能为空'),
                    array(check::must($_POST['name']),'字段标识不能为空'),
                    array(check::must($_POST['fields']),'字段名不能为空'),
                    array(check::userName($_POST['fields'],3,20,'EN'),'只能输入3至20个纯英文字母'));
            if(true !== $msg){
                $this->jsonMsg($msg,1);
            }
            $mid = intval($_POST['mid']);
            $data = array();
            $_POST['fields'] = 'ext_'.$_POST['fields'];
            $_POST['uptime'] = time();
            $data = postinput($_POST,array('fid'),1);
            $result = model('field')->addField($data);
            if($result){
                $this->jsonMsg('字段添加成功',1,url('module/field',array('id'=>$mid)));  
            }else{
                $this->jsonMsg('字段添加失败,查看是否添加相同字段'); 
            }
        }else{
            $fid = intval($_GET['id']);
            $this->pid = $fid;
            $this->display('module/fieldadd');
        }
    }
    //管理模型字段
    public function fieldedit(){
            if($this->isPost()){
                $data = array();
                $data = postinput($_POST,array('fid'));
                $fid = intval($_POST['fid']);
                $mid = intval($_POST['mid']);
                $result = model('field')->fieldedit($fid,$data);
                if($result){
                    $this->jsonMsg('字段信息修改成功',1,url('module/field',array('id'=>$mid)));
                }else{
                    $this->jsonMsg('字段修改失败');
                }
            }else{
                $fid= intval($_GET['id']); 
                $this->info = model('field')->find('id = '.$fid);
                if($this->info){
                    $str = explode('_',$this->info['fields']);
                    $this->fields = $str[1];
                }
                $this->display('module/fieldedit');   
            }
    } 
    //字段排序
    public function uporter(){
            $fid=  intval($_POST['id']);
            $condition['id'] = $fid;
            $data['neworder'] = intval($_POST['sorter']);
            $info = model('field')->update($condition,$data);
            if($info){
                $this->jsonMsg('模型字段自定义排序成功',1);
            }else{
                $this->jsonMsg('模型字段自定义排序失败');
            }
    }
}